import os
import Image

from damn.util import GetScriptPath, RunBlenderURI, BlenderDependency

from damn.transcoder import TranscoderFailedException, BaseTranscoder

BlenderDependency()

class BlenderMeshTranscoder(BaseTranscoder):
  transcoder = True
  convert_map = {"application/x-blender.object" : "application/x-crystalspace.library+xml"}

  def transcode(self, anAssetReference, dstFormat, **kwargs):
    filePath = super(BlenderMeshTranscoder, self).transcode(anAssetReference, dstFormat, kwargs)
    environ = {}
    
    dependencies={}
    for asset in anAssetReference.dependencies:
      if asset.format not in dependencies: dependencies[asset.format] = {}
      dependencies[asset.format][asset.subName] = (str(asset.id), asset.file.fileName,)
    environ["BlenderMeshTranscoder-dependencies"] = repr(dependencies)
    
    environ["BlenderMeshTranscoder-id"] = str(anAssetReference.id)
    environ["BlenderMeshTranscoder-object"] = anAssetReference.subName
    environ["BlenderMeshTranscoder-path"] = filePath
    
    stdoutdata, stderrdata, returncode = RunBlenderURI(anAssetReference.file.fileName, GetScriptPath(__file__), None, environ)
    
    if returncode != 0: raise TranscoderFailedException('BlenderMeshTranscoder failed! (return code %d)!'%(returncode))
    
    return filePath    
    
    
    
    
    
    
    
